#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=50, inf=0x7ffffff;

struct Graph {
    int n, m, ind[maxn+1], to[maxn*maxn+1], link[maxn*maxn+1], val[maxn*maxn+1];

    void init(int size) {
        n = size;
        m = 0;
        memset(ind, 0, sizeof ind);
    }

    void addSide(int u, int v, int w) {
        m++;
        to[m] = v;
        link[m] = ind[u];
        ind[u] = m;
    }
};

int n, a[maxn+1], b[maxn+1];

int getAns(Graph &g) {
    int ret=0;
    static Graph ng;
    static int out[maxn+1], in[maxn+1], s[maxn+1], map[maxn+1];
    static bool vis[maxn+1];

    for (bool flag=true; flag; ) {

        g.val[0] = inf;
        flag = false;
        for (int i=1; i<=g.n; i++) {
            in[i] = out[i] = 0;
            for (int j=g.ind[i]; j; j=g.link[j]) {
                if (g.val[out[i]]>g.val[j]) {
                    out[i] = j;
                }
            }
        }

        for (int i=1; i<=g.n; i++) ret+=g.val[out[i]];

        memset(vis, 0, sizeof vis);
        memset(map, 0, sizeof map);
        int cnt=0;
        for (int i=1; i<=g.n; i++) {
            if (!vis[i]) {
                s[0] = 0;
                for (int o=i; o; o=g.to[out[o]]) {
                    vis[o] = true;
                    s[++s[0]] = o;
                    int no=g.to[out[o]];
                    if (vis[no]) {
                        flag = true;
                        in[no] = out[o];
                        map[no] = ++cnt;
                        for (int j=s[0]; s[j]!=no; j--) {
                            in[s[j]] = out[s[j-1]];
                            map[s[j]] = cnt;
                        }
                        break;
                    }
                }
                for (int j=1; j<=s[0] && !map[s[j]]; j++) map[s[j]] = ++cnt;
            }
        }

        ng.init(cnt);
        for (int i=1; i<=g.n; i++) {
            for (int j=g.ind[i]; j; j=g.link[j]) {
                if (map[i]!=map[g.to[j]]) {
                    ng.addSide(map[i], map[g.to[j]], g.val[j]-g.val[in[g.to[j]]]-g.val[out[i]]);
                }
            }
        }
        g = ng;
    }
    return ret;
}

int main() {
    freopen("pl.in", "r", stdin);
    freopen("pl.out", "w", stdout);

    scanf("%d", &n);
    for (int i=1; i<=n; i++) scanf("%d", a+i);
    for (int i=1; i<=n; i++) scanf("%d", b+i);

    int ans;
    static Graph g;
    g.init(n);
    for (int i=1; i<=n; i++) {
        for (int j=1; j<=n; j++) {
            if (i==j) g.addSide(i, 0, a[i]^b[i]);
            else g.addSide(i, j, a[i]^b[j]);
        }
    }
    ans = getAns(g);

    static int p[maxn+1];
    static bool book[maxn+1];
    for (int i=1; i<=n; i++) {
        for (int o=1; o<=n; o++) {
            if (!book[o]) {
                g.init(n);
                for (int j=1; j<i; j++) {
                    g.addSide(p[j], 0, a[p[j]]^b[p[j]]);
                    for (int k=1; k<j; k++) {
                        g.addSide(p[j], p[k], a[p[j]]^b[p[k]]);
                    }
                }
                g.addSide(o, o, a[o]^b[o]);
                for (int j=1; j<i; j++) {
                    g.addSide(o, p[j], a[o]^b[p[j]]);
                }

                for (int j=1; j<=n; j++) {
                    if (!book[j]) {
                        for (int k=1; k<=n; k++) {
                            if (k==j) g.addSide(j, 0, a[j]^b[j]);
                            else g.addSide(j, k, a[j]^b[k]);
                        }
                    }
                }

                if (getAns(g)==ans) {
                    p[i] = o;
                    book[o] = true;
                    break;
                }
            }
        }
    }

    printf("%d\n", ans);
    for (int i=1; i<=n; i++) {
        printf("%d ", p[i]);
    }
    printf("\n");

    fclose(stdin);
    fclose(stdout);
    return 0;
}
